# import cv2
#
# img = cv2.imread('./01.png')
# img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#
# img_gray = cv2.resize(img_gray,(200,200))
#
# # cv2.imshow('img_gray',img_gray)
# t,res = cv2.threshold(img_gray,
#                       127,
#                       255,
#                       cv2.THRESH_BINARY)
# cv2.imshow('res',res)
#
# cv2.waitKey()
# cv2.destroyAllWindows()

"""
二值化：
    将所有元素，与阈值进行比较，转成只有0和1的样本
"""
import numpy as np
import sklearn.preprocessing as sp

raw_sample = np.array([[60.8,90.6,100.5],
                       [56.7,7.8,12.3],
                       [66.6,38.6,99.9]])

bin_sample = raw_sample.copy()
# 将小于60的转为0，大于等于60的转为0
# 生成掩码数组
mask1 = bin_sample < 60
mask2 = bin_sample >=60
# 通过掩码进行二值化处理
bin_sample[mask1] = 0
bin_sample[mask2] = 1
print(bin_sample)

# 使用sklearn提供API实现二值化
biner = sp.Binarizer(threshold=59)
res = biner.transform(raw_sample)
print("Sklearn")
print(res)